package org.qufenqi.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.qufenqi.api.common.CommonResponse;
import org.qufenqi.api.entity.Category;
import org.qufenqi.api.entity.Item;
import org.qufenqi.api.entity.Product;
import org.qufenqi.api.mapper.CategoryMapper;
import org.qufenqi.api.mapper.ItemMapper;
import org.qufenqi.api.mapper.ProductMapper;
import org.qufenqi.api.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;


@Service("CatalogService")
public class CatalogServiceimpl implements CatalogService {
    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ItemMapper itemMapper;

    public CommonResponse<Category> getCategorybyId(String id)
    {
        Category category = categoryMapper.selectById(id);
        if(category == null)
        {
            return CommonResponse.responseForErrorMessage("未找到该种类");
        }
        return CommonResponse.responseForSuccess(category);
    }

    @Override
    public CommonResponse<Product> getProductbyId(String id) {
        Product product = productMapper.selectById(id);
        if(product == null)
        {
            return CommonResponse.responseForErrorMessage("未找到该品牌");
        }
        return CommonResponse.responseForSuccess(product);
    }

    @Override
    public CommonResponse<Item> getItembyId(String id) {
        Item item = itemMapper.selectById(id);
        if(item == null)
            return CommonResponse.responseForErrorMessage("没有该商品");
        return CommonResponse.responseForSuccess(item);
    }

    @Override
    public CommonResponse<List<Item>> query(String key) {
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        wrapper.like("itemsort",key).or().like("productid",key).or().like("itemname",key);
        List<Item> list = itemMapper.selectList(wrapper);
        if(list.isEmpty()){
            return CommonResponse.responseForErrorMessage("没有相关的商品");
        }
        return CommonResponse.responseForSuccess(list);
    }

    @Override
    public CommonResponse<List<Item>> getItembyProduct(String id1, String id2) {
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        wrapper.eq("itemsort",id1);
        wrapper.eq("productid",id2);
        List<Item> list = itemMapper.selectList(wrapper);
        if(list.isEmpty())
            return CommonResponse.responseForErrorMessage("未找到该种商品");
        return CommonResponse.responseForSuccess(list);
    }

    @Override
    public CommonResponse<List<Item>> getItembyCategory(String id) {
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        wrapper.eq("itemsort",id);
        List<Item> list = itemMapper.selectList(wrapper);
        if(list.isEmpty())
        {
            return CommonResponse.responseForErrorMessage("没有该种类的商品");
        }
        return CommonResponse.responseForSuccess(list);
    }
}
